package com.test.nnooolwdeaer.view_wooden.info_input_wooden;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.test.nnooolwdeaer.R;
import com.test.nnooolwdeaer.adapter_wooden.InfoInputListAdapter_wooden;
import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.StringUtil_wooden;
import com.test.nnooolwdeaer.util_wooden.http_wooden.HttpManager_wooden;
import com.test.nnooolwdeaer.view_wooden.TrackEditText;
import com.test.nnooolwdeaer.view_wooden.base_wooden.BaseInfoInputViewManager_wooden;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class ContactViewManager_wooden extends BaseInfoInputViewManager_wooden {

    private final TextView relationshipTv;
    private final TrackEditText relationshipEt;
    private final View relationshipSelectView;
    private final ListView relationshipLv;
    private final TextView nameTv;
    private final TextView mobileTv;

    private long startTime = 0;

    private int itemHeight_wooden = 0;
    private final ArrayList<String> relationshipItems;

    @SuppressLint("ClickableViewAccessibility")
    public ContactViewManager_wooden(View view, int count, ArrayList<String> relationshipLv, Runnable onContactSelectRunnable){
        super(view,"");

        TextView titleTv = mView_wooden.findViewById(R.id.view_contact_title);
        View relationshipInputView = mView_wooden.findViewById(R.id.view_contact_view_relationship_input);
        relationshipTv = mView_wooden.findViewById(R.id.view_contact_tv_relationship);
        relationshipEt = mView_wooden.findViewById(R.id.view_contact_et_relationship);
        relationshipSelectView = mView_wooden.findViewById(R.id.view_contact_view_relationship_select);
        this.relationshipLv = mView_wooden.findViewById(R.id.view_contact_layout_relationship_item);
        View nameView = mView_wooden.findViewById(R.id.view_contact_view_name);
        nameTv = mView_wooden.findViewById(R.id.view_contact_tv_name);
        View mobileView = mView_wooden.findViewById(R.id.view_contact_view_mobile);
        mobileTv = mView_wooden.findViewById(R.id.view_contact_tv_mobile);

        relationshipEt.sTrackWord("CONTACT_INFO_RELATIONSHIP_INPUT");

        if (count>1) titleTv.setText(mView_wooden.getContext().getString(R.string.contact_title_2,count));

        this.relationshipItems = relationshipLv;

        if (relationshipItems !=null){

            final String others = mView_wooden.getContext().getString(R.string.others);
            if (!relationshipItems.contains(others)) relationshipItems.add(others);

            setRelationshipListItem((parent, view1, position, id) -> {
                String relationship = relationshipItems.get(position);
                long endTime = System.currentTimeMillis();
                HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("CONTACT_INFO_RELATIONSHIP_SELECT",relationship+"$$"+(endTime-startTime));
                relationshipTv.setText(relationship);
                relationshipSelectView.setVisibility(View.GONE);
                if (others.equalsIgnoreCase(relationship)){
                    relationshipEt.setVisibility(View.VISIBLE);
                }else relationshipEt.setVisibility(View.GONE);

            });
        }

        relationshipInputView.setOnClickListener(v -> {

            if (relationshipSelectView.getVisibility() == View.GONE){
                startTime = System.currentTimeMillis();
                relationshipSelectView.setVisibility(View.VISIBLE);
            }else relationshipSelectView.setVisibility(View.GONE);

        });

        nameView.setOnClickListener(v -> {
            startTime = System.currentTimeMillis();
            if (onContactSelectRunnable!=null) onContactSelectRunnable.run();
        });

        mobileView.setOnClickListener(v -> {
            startTime = System.currentTimeMillis();
            if (onContactSelectRunnable!=null) onContactSelectRunnable.run();
        });

    }

    public void setRelationshipListItem(AdapterView.OnItemClickListener listener){
        InfoInputListAdapter_wooden listAdapter_wooden = new InfoInputListAdapter_wooden(mView_wooden.getContext(), relationshipItems);
        relationshipLv.setAdapter(listAdapter_wooden);
        relationshipLv.setOnItemClickListener(listener);

        ViewGroup.LayoutParams layoutParams = relationshipLv.getLayoutParams();
        if (itemHeight_wooden == 0) itemHeight_wooden = layoutParams.height;
        layoutParams.height = relationshipItems.size()* itemHeight_wooden;
        relationshipLv.setLayoutParams(layoutParams);
    }

    @Override
    protected void init_wooden(){}

    public void contactSet(String name, String mobile){
        name = StringUtil_wooden.getSafeString_wooden(name);
        mobile = StringUtil_wooden.getSafeString_wooden(mobile);
        long endTime = System.currentTimeMillis();
        HttpManager_wooden.getInstance_wooden().saveUserBuriedPoint("CONTACT_INFO_CONTACT_INPUT",name+":"+mobile+"$$"+(endTime-startTime));
        nameTv.setText(name);
        mobileTv.setText(mobile);
    }

    @Override
    public boolean checkInput_wooden() {
        String relationship = relationshipTv.getText().toString();
        if ("Others".equalsIgnoreCase(relationship) || "Autres".equalsIgnoreCase(relationship) || "Người khác".equalsIgnoreCase(relationship))
            relationship = relationshipEt.gText();
        String name = nameTv.getText().toString();
        String mobile = mobileTv.getText().toString();
        return !TextUtils.isEmpty(relationship) && !TextUtils.isEmpty(name) && !TextUtils.isEmpty(mobile);
    }

    @Override
    public String getInput_wooden() {
        String relationship = relationshipTv.getText().toString();
        if ("Others".equalsIgnoreCase(relationship) || "Autres".equalsIgnoreCase(relationship) || "Người khác".equalsIgnoreCase(relationship))
            relationship = relationshipEt.gText();
        String name = nameTv.getText().toString();
        String mobile = mobileTv.getText().toString();


        JSONObject jsonObject = new JSONObject();

        try {
            jsonObject.put("relation",StringUtil_wooden.getSafeString_wooden(relationship));
            jsonObject.put("fullName",StringUtil_wooden.getSafeString_wooden(name));
            jsonObject.put("mobile",StringUtil_wooden.getSafeString_wooden(mobile));
        } catch (JSONException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }

        return jsonObject.toString();
    }

    public JsonObject getContactJson(){

        String relationship = relationshipTv.getText().toString();
        if ("Others".equalsIgnoreCase(relationship) || "Autres".equalsIgnoreCase(relationship) || "Người khác".equalsIgnoreCase(relationship))
            relationship = relationshipEt.gText();
        String name = nameTv.getText().toString();
        String mobile = mobileTv.getText().toString();


        JsonObject jsonObject = new JsonObject();

        jsonObject.addProperty("relation",StringUtil_wooden.getSafeString_wooden(relationship));
        jsonObject.addProperty("fullName",StringUtil_wooden.getSafeString_wooden(name));
        jsonObject.addProperty("mobile",StringUtil_wooden.getSafeString_wooden(mobile));

        return jsonObject;

    }

}
